//
//  RightViewController.m
//  ProjectA_V0.1
//
//  Created by liuchang on 14/12/29.
//  Copyright (c) 2014年 liuchang. All rights reserved.
//

#import "RightViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface RightViewController ()

@end

@implementation RightViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addRecognizer];
    [self setUI];
}

- (void)setUI
{
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.05 alpha:0.95]];
}

- (void)addRecognizer
{
    UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerAction:)];
    [self.view addGestureRecognizer:panRecognizer];
    [panRecognizer release];
}

- (void)recognizerAction:(id)sender
{
    UIPanGestureRecognizer * panRecognizer = (UIPanGestureRecognizer *)sender;
    CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
    if (UIGestureRecognizerStateBegan == panRecognizer.state)
    {
#warning 快速拖动
    }
    if (UIGestureRecognizerStateBegan == panRecognizer.state ||
        UIGestureRecognizerStateChanged == panRecognizer.state) {
        if (self.view.frame.origin.x + 0.618 * SCREEN_WIDTH + translation.x > SCREEN_WIDTH) {
            self.view.center = CGPointMake(self.view.center.x + translation.x, self.view.center.y);
            [panRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
        }
        else {
            [self.view setFrame:CGRectMake(SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
        }
    }
    if (UIGestureRecognizerStateEnded == panRecognizer.state) {
        if (self.view.frame.origin.x > SCREEN_WIDTH * 0.618 - 50) {
            [UIView animateWithDuration:0.3 animations:^{
                [self.view setFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
                self.endBlock();
            }];
        }
        else {
            [UIView animateWithDuration:0.3 animations:^{
                [self.view setFrame:CGRectMake(SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.618, SCREEN_HEIGHT)];
            }];
            
        }
    }
    
}

- (void)endGetBlock:(EndBlock)endBlock
{
    self.endBlock = endBlock;
}


@end
